//
//  FMDBCollectionManager.m
//  takeout
//
//  Created by Mac on 12-4-18.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import "FMDBCollectionManager.h"
#import "FMDatabase.h"
#import "FMDBManager.h"
#import "Collection.h"
#import "Supplier.h"
#import "Constants.h"
@implementation FMDBCollectionManager
//获取所有收藏店铺
-(NSArray *) getAllCollection{
    FMDBManager *fmdbManager= [[FMDBManager alloc]init];
    FMDatabase *db=[fmdbManager getDataBase];
    FMResultSet *rs = [db executeQuery:@"SELECT * FROM collections"];
    NSMutableArray *collections=[NSMutableArray arrayWithCapacity:2];
    while ([rs next]) {
        Collection *collection=[[Collection alloc]init];
        collection.collectionId=[rs intForColumn:@"id"];
        collection.supplierId=[rs intForColumn:@"supplierid"];
        collection.supplierName=[rs stringForColumn:@"name"];
        collection.imgUrl=[rs stringForColumn:@"picture"];
        collection.page=[rs intForColumn:@"page"];
        [collections addObject:collection];
    }    
    [rs close];
//    [fmdbManager closeDataBase];
    return collections;
}

//第一页是否满了
-(BOOL)getIsFullByPage{
    BOOL ifFull;
    FMDBManager *fmdbManager= [[FMDBManager alloc]init];
    FMDatabase *db=[fmdbManager getDataBase];
    FMResultSet *rs = [db executeQuery:@"SELECT * FROM collections where page=1"];
    NSMutableArray *collections2=[NSMutableArray arrayWithCapacity:2];
    while ([rs next]) {
        Collection *collection=[[Collection alloc]init];
        collection.collectionId=[rs intForColumn:@"id"];
        collection.supplierId=[rs intForColumn:@"supplierid"];
        collection.supplierName=[rs stringForColumn:@"name"];
        collection.imgUrl=[rs stringForColumn:@"picture"];
        collection.page=[rs intForColumn:@"page"];
        [collections2 addObject:collection];
    }    
    [rs close];
//    [fmdbManager closeDataBase];
    
    
    //第一页满了，不能添加
    if([collections2 count]>=SScollectionImg)
    {
        ifFull=true;
    }
    //第一页未满，可以添加
    else
    {
        ifFull=false;
    }
    return ifFull;
    
    
}

//插入收藏店铺
-(BOOL) insertCollection:(Supplier *)supplier{
    FMDBManager *fmdbManager= [[FMDBManager alloc]init];
    FMDatabase *db=[fmdbManager getDataBase];
    FMResultSet *rs = [db executeQuery:@"SELECT * FROM collections where supplierid=?",[NSNumber numberWithInt:supplier.supplierId]];
    if([rs next]){
//        [fmdbManager closeDataBase];
        return false;
    }
    else{
        BOOL isInsert=[self getIsFullByPage];
        if(isInsert==false)
            //第一页未满，插入第一页
        {
            [db executeUpdate:@"INSERT INTO collections(supplierid,name,picture,page) values(?,?,?,?)",[NSNumber numberWithInt:[supplier supplierId]],supplier.supplierName,supplier.imgUrl,@"1"];
        }
        else
            //插入第二页
        {
            [db executeUpdate:@"INSERT INTO collections(supplierid,name,picture,page) values(?,?,?,?)",[NSNumber numberWithInt:[supplier supplierId]],supplier.supplierName,supplier.imgUrl,@"2"];
        }
//        [fmdbManager closeDataBase];
        return true;
    }
    return false;
}

//插入收藏店铺(用于收藏页面)
-(BOOL) insertCollection:(NSString *)supplierId name:(NSString *)name imgUrl:(NSString *)imgUrl page:(NSString *)page{
    FMDBManager *fmdbManager= [[FMDBManager alloc]init];
    FMDatabase *db=[fmdbManager getDataBase];
    FMResultSet *rs = [db executeQuery:@"SELECT * FROM collections where supplierid=?",supplierId];
    if([rs next]){
//        [fmdbManager closeDataBase];
        return false;
    }
    else{
        [db executeUpdate:@"INSERT INTO collections(supplierid,name,picture,page) values(?,?,?,?)",supplierId,name,imgUrl,page];
//        [fmdbManager closeDataBase];
        return true;
    }
    return false;
}

//清除所有店铺
-(void) deleteAllCollection{
    FMDBManager *fmdbManager= [[FMDBManager alloc]init];
    FMDatabase *db=[fmdbManager getDataBase];
    [db executeUpdate:@"DELETE FROM collections"];
//    [fmdbManager closeDataBase];
}


@end
